using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using WatiN.Core;
using Wax;

namespace WaxLibTest
{
    [TestFixture]
    public class TextFieldCommandTest
    {
        /// <summary>
        /// Tests the type text command.
        /// </summary>
        [Test]
        public void TypeText()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "TypeTextTest", "TypeText", "Eddie", "ById", "FirstName" }));
                testManager.Execute();

                Assert.AreEqual("Eddie", testManager.Browser.TextField("FirstName").Text);
            }
        }

        /// <summary>
        /// Tests the select text command.
        /// </summary>
        [Test]
        public void SelectText()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "TypeTextTest", "TypeText", "Eddie", "ById", "FirstName" }));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "SelectTextTest", "SelectText", string.Empty, "ById", "FirstName" }));
                testManager.Execute();
            }
        }

        /// <summary>
        /// Tests the clear text command.
        /// </summary>
        [Test]
        public void ClearText()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "TypeTextTest", "TypeText", "Eddie", "ById", "FirstName" }));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "ClearTest", "ClearText", string.Empty, "ById", "FirstName" }));
                testManager.Execute();

                Assert.AreEqual(null, testManager.Browser.TextField("FirstName").Text);
            }
        }

        /// <summary>
        /// Tests the append text command.
        /// </summary>
        [Test]
        public void AppendText()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "TypeTextTest", "TypeText", "Eddie", "ById", "FirstName" }));                
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "AppendTextTest", "AppendText", " Wilde", "ById", "FirstName" }));
                testManager.Execute();

                Assert.AreEqual("Eddie Wilde", testManager.Browser.TextField("FirstName").Text);
            }
        }

        /// <summary>
        /// Tests that the correct exception is thrown if a type text command is issued for an element that is not a text input element.
        /// </summary>
        [Test, ExpectedException(typeof(ArgumentException))]
        public void TypeTextInvalidElementSpecified()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1.htm")));
                testManager.Tests[0].Commands.Add(CommandFactory.Parse(new String[] { "TypeTextTest", "TypeText", "Eddie", "ById", "chkEmailMe" }));
                testManager.Execute();

                Assert.AreEqual("Eddie", testManager.Browser.TextField("FirstName").Text);
            }            
        }
    }
}
